package tools;

import java.awt.image.BufferedImage;

public class Image {
	public static double[] getScalarLuminance(int[] lum, double[] fillme) {
		for (int i = 0; i<fillme.length; i++) {
			fillme[i] = lum[i]/255.;
		}
		return fillme;
	}
	public static final double[] sobely = {-1,-2,-1,0,0,0,1,2,1}; 
	public static final double[] dy = {-1,-1,1,1};
	public static final double[] sobelx = {-1,0,1,-2,0,2,-1,0,1};
	public static final double[] mean3x3 = {1,1,1,1,1,1,1,1,1};
	public static final double[] mean5x5 = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
	public static final double[] zero = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};

	public static final double[] haargausxy = {	0,0,0,0,0,0,0,0,0,
												0,-1,-1,-1,0,1,1,1,0,
												0,-1,-1,-1,0,1,1,1,0,
												0,-1,-1,-1,0,1,1,1,0,
												0,0,0,0,0,0,0,0,0,
												0,1,1,1,0,-1,-1,-1,0,
												0,1,1,1,0,-1,-1,-1,0,
												0,1,1,1,0,-1,-1,-1,0,
												0,0,0,0,0,0,0,0,0
												};
	public static final double[] haargausxx = {	0,0,1,1,1,1,1,0,0,
												0,0,1,1,1,1,1,0,0,
												0,0,1,1,1,1,1,0,0,
												0,0,-2,-2,-2,-2,-2,0,0,
												0,0,-2,-2,-2,-2,-2,0,0,
												0,0,-2,-2,-2,-2,-2,0,0,
												0,0,1,1,1,1,1,0,0,
												0,0,1,1,1,1,1,0,0,
												0,0,1,1,1,1,1,0,0,
												};

	
	public static double[] conv(double[] img, int w, int h, double[] kernel, int wk, int hk, double[] fillme) {
		int nx = wk/2, ny = hk/2;

		for (int y = 0; y<h; y++) {
			for (int x = 0; x<w; x++) {
				double sum = 0;
				for (int j = -ny; j<ny; j++) {
					for (int i = -nx; i<nx; i++) {
						int index = ((x+w+i)%w)+((y+h+j)%h)*w;
						sum += img[index]*kernel[i+nx+(j+ny)*wk];
					}
				}
				fillme[x+y*w] = sum;
			}
		}
		return fillme;
	}
	public static int bilerp(int[] pix, double x, double y, int w, int h) {
		if (x>w || y>h || x<0 || y<0)
			return 0;
		double down = Math.min(h-1, Math.ceil(y)), up = Math.floor(y), left = Math.floor(x), right = Math.min(w-1,Math.ceil(x));
		try {
			double i1 = pix[(int)(left+up*w)], i2 = pix[(int)(right+up*w)], i3 = pix[(int)(left+down*w)], i4 = pix[(int)(right+down*w)];
			double x1 = (1-(x-left))*i1+(x-left)*i2, x2 = (1-(x-left))*i3+(x-left)*i4;
			int val = (int)(x2*(y-up)+x1*(1-(y-up)));
//			System.out.println(val);
//			System.out.println(val);
			return val;
			
		}
		catch (Exception e) {
			e.printStackTrace();
			System.out.println(up+" "+down+" "+left+" "+right+" "+(y-up)+" "+(x-left));
		}
		return 0;
	}
	public static double bilerp(double[] pix, double x, double y, int w, int h) {
		
		if (x>w || y>h || x<0 || y<0)
			return 0;
		double down = Math.min(h-1, Math.ceil(y)), up = Math.floor(y), left = Math.floor(x), right = Math.min(w-1,Math.ceil(x));
		try {
			double i1 = pix[(int)(left+up*w)], i2 = pix[(int)(right+up*w)], i3 = pix[(int)(left+down*w)], i4 = pix[(int)(right+down*w)];
			double x1 = (1-(x-left))*i1+(x-left)*i2, x2 = (1-(x-left))*i3+(x-left)*i4;
			double val = (x2*(y-up)+x1*(1-(y-up)));
//			System.out.println(val);
//			System.out.println(val);
			return val;
			
		}
		catch (Exception e) {
			e.printStackTrace();
			System.out.println(up+" "+down+" "+left+" "+right+" "+(y-up)+" "+(x-left));
		}
		return 0;
	}
	public static int[] prepare(double[] src, int[] fillme) {
		double max = Double.NEGATIVE_INFINITY, min = Double.POSITIVE_INFINITY;
		
		for (int i = 0; i<src.length; i++) {
			max = Math.max(max, src[i]);
			min = Math.min(min, src[i]);
		}
		
		for (int i = 0; i<src.length; i++) {
			fillme[i] = (int)(255.*((src[i]-min)/(max-min)));
		}
		
		return fillme;
	}
	public static int[] buildRGB(double[] r, double[] g, double[] b, int[] fillme) {
		
		for (int i = 0; i<r.length; i++) {
			
			int red = 0, green = 0, blue = 0;
			
			if (r!=null) 
				red = (int)(r[i]*255.);
			if (g!=null)
				green = (int)(g[i]*255.);
			if (b!=null)
				blue = (int)(b[i]*255.);
			
			fillme[i] = red<<16 | green<<8 | blue | 0xFF000000;
		}
		return fillme;
	}
	public static int[] buildRGB(int[] r, int[] g, int[] b, int[] fillme) {
		
		for (int i = 0; i<r.length; i++) {
			
			int red = 0, green = 0, blue = 0;
			
			if (r!=null) 
				red = (r[i]);
			if (g!=null)
				green = (g[i]);
			if (b!=null)
				blue = (b[i]);
			
			fillme[i] = red<<16 | green<<8 | blue | 0xFF000000;
		}
		return fillme;
	}
}
